package com.lxj.core.action;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.lxj.common.utils.Page;
import com.lxj.core.entity.SYSBaseEntity;
import com.lxj.core.service.IBaseService;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class BaseAction extends ActionSupport {
	protected  IBaseService baseService;
	private Page pager = new Page();
	private int maxPage = 10;
	private String pageUrl = "#";

	public Criteria createCriteria(Class<? extends SYSBaseEntity> clazz) {
		return this.baseService.getBaseDao().getSession().createCriteria(clazz);
	}

	public Query createQuery(String hql) {
		return this.baseService.getBaseDao().getSession().createQuery(hql);
	}
	/**
	 * 
	 * @param criteria
	 *            在线查询条件
	 * @param queryActionURL
	 *            分页用的Action地址
	 * @param maxPage
	 *            第页显示记录数
	 */
	public void queryByPage(Criteria criteria, String queryActionURL, int maxPage) {
		this.setPageUrl(queryActionURL);
		pager = this.baseService.findBy(criteria, pager.getOffset() + 1, maxPage);

	}

	/**
	 * 
	 * @param query
	 *            在线查询条件
	 * @param queryActionURL
	 *            分页用的Action地址
	 * @param maxPage
	 *            第页显示记录数
	 */
	public void queryByPage(Query query, String queryActionURL, int maxPage) {
		this.setPageUrl(queryActionURL);
		pager = this.baseService.findBy(query, pager.getOffset() + 1, maxPage);
	}

	/**
	 * 
	 * @param query
	 *            在线查询条件
	 * @param queryActionURL
	 *            分页用的Action地址
	 * @param maxPage
	 *            第页显示记录数
	 */
	public void queryByPage(String sql, String queryActionURL, int maxPage) {
		this.setPageUrl(queryActionURL);
		pager = this.baseService.findBy(sql, pager.getOffset() + 1, maxPage);
	}

	public Page getPager() {
		return pager;
	}

	public void setPager(Page pager) {
		this.pager = pager;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	@Resource
	public void setBaseService(IBaseService baseService) {
		this.baseService = baseService;
	}

	public IBaseService getBaseService() {
		return baseService;
	}
}
